
function showModalPage(url, width, height, callbackbutton, returnto) {
    if (url.lastIndexOf("?") == -1) {
        url += "?";
    }
    url += "&" + (new Date()).valueOf();
    var returnvalue = window.showModalDialog(url, 'window', 'dialogWidth: ' + width + 'px; dialogHeight: ' + height + 'px; dialogTop: px; dialogLeft: px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No;scroll:No');
    if (returnvalue != null) {
        document.getElementById(returnto).value = returnvalue;
        document.getElementById(callbackbutton).click();
    }
}
function showModalPage2(url, width, height) {
    if (url.lastIndexOf("?") == -1) {
        url += "?";
    }
    url += "&" + (new Date()).valueOf();
    window.showModalDialog(url, 'window', 'dialogWidth: ' + width + 'px; dialogHeight: ' + height + 'px; dialogTop: px; dialogLeft: px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No;scroll:Yes');
}

function showMaxModalPage(url) {
    var width = window.screen.availWidth;
    var height = window.screen.availHeight;
    if (url.lastIndexOf("?") == -1) {
        url += "?";
    }
    url += "&" + (new Date()).valueOf();
    window.showModalDialog(url, 'window', 'dialogWidth: ' + width + 'px; dialogHeight: ' + height + 'px; dialogTop: px; dialogLeft: px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No;scroll:Yes');

}

function showPage(url, width, height) {
    if (url.lastIndexOf("?") == -1) {
        url += "?";
    }
    url += "&" + (new Date()).valueOf();
    var newwin = window.open(url, '', 'width=' + width + ',height=' + height + ',location=no,menubar=0,titlebar=0,toolbar=0,scrollbars=1,resizable=1');
    newwin.focus();
}

function showSaleBill(id, flg) {
    showModalPage("../Sale/SaleVoucherView.aspx?id=" + id + "&f=" + flg, 900, 600, null, null);
}

function showPurchaseBill(id, flg) {
    showModalPage("../Purchase/PurchaseOrderView.aspx?id=" + id + "&f=" + flg, 900, 600, null, null);
}

function showMaterialBill(id, flg) {
    showModalPage("../Warehouse/MaterialRequisitionView.aspx?id=" + id + "&f=" + flg, 900, 600, null, null);
}
//退货单
function showReturnBill(id, flg) {
    showModalPage("../Sale/ReturnGoodsNoteView.aspx?id=" + id + "&f=" + flg, 900, 600, null, null);
}
//采购退货单
function showPurchaseReturnBill(id, flg) {
    showModalPage("../Purchase/ReturnGoodsNoteView.aspx?id=" + id + "&f=" + flg, 900, 600, null, null);
}

//返回数字
function $id(str) {
    return document.getElementById(str);
}
//返回数字
function getNum(str) {
    if (trim(str) == "")
    { return 0; }
    else {
        return isNaN(str) ? 0 : parseFloat(str);
    }
}

//固定小数位
if (!Number.prototype.toFixed) {
    Number.prototype.toFixed = function(n) {
        with (Math) return round(Number(this) * pow(10, n)) / pow(10, n)
    }
}

//trim
function trim(s) {
    var flg = 0;
    var lstr = '';
    while (s.charCodeAt(flg) == 32 || s.charCodeAt(flg) == 160) flg++;
    for (var index = 0; index < s.length - flg; index++)
        lstr += s.charAt(flg + index);
    return lstr;
}
//为表格中的文本框添加自动计算功能
//tbl:需要绑定的表格ID
//col1:被加列1
//col2:被加列2
//col3:结果列
//小数位数
//计算类型符号(默认为加法):"and","min","mul","div"
function dynamicCaculor(tbl, col1, col2, resultCol, decCount, operation) {
    //保持原始数据
    jQuery("#" + tbl + " :text:not(:hidden)").each(function() {
        jQuery(this).data("old_value", jQuery(this).val());
    }
    );
    //为文本框添加焦点离开事件
    jQuery("#" + tbl + " tr").each(function() {
        var tr = this;
        var $td = jQuery(this).find("td");
        jQuery(this).find("input:text").blur(function() {
            var tmp = jQuery.trim(jQuery(this).val());
            if (tmp == "")
            { jQuery(this).val(0); }
            if (isNaN(tmp)) {
                //非数字类型时，自动还原前次内容
                jQuery(this).val(jQuery(this).data("old_value"));
                return;
            }
            else {
                jQuery(this).val(getNum(jQuery(this).val()));
                jQuery(this).data("old_value", jQuery(this).val());
            }
            var sum = 0;
            var num1, num2;
            num1 = getNum(jQuery.trim(jQuery($td[col1]).text())) + getNum(jQuery.trim(jQuery($td[col1]).find("input:text:not(.none)").val()));
            num2 = getNum(jQuery.trim(jQuery($td[col2]).text())) + getNum(jQuery.trim(jQuery($td[col2]).find("input:text:not(.none)").val()));
            switch (operation) {
                case "min":
                    sum = num1 - num2; break;
                case "mul":
                    sum = num1 * num2; break;
                case "div":
                    sum = num1 / num2; break;
                default:
                    sum = num1 + num2;
            }
            sum = sum.toFixed(decCount);
            //判断结果列是否是文本框
            if (jQuery($td[resultCol]).find("input:text").size())
            { jQuery($td[resultCol]).find("input:text").val(sum); }
            else
            { jQuery($td[resultCol]).text(sum); }
        }
);
    }
);
}

//为表格中的文本框添加自动计算功能
//tbl:需要绑定的表格ID
//col1:被加列1
//col2:被加列2
//col3:结果列
//小数位数
//计算类型符号(默认为加法):"and","min","mul","div"
function dynamicCaculor2(tbl, col1, col2, resultCol, decCount, operation) {
    //保持原始数据
    jQuery("#" + tbl + " :text:not(:hidden)").each(function() {
        jQuery(this).data("old_value", jQuery(this).val());
    }
    );
    //为文本框添加焦点离开事件
    jQuery("#" + tbl + " tr").each(function() {
        var tr = this;
        var $td = jQuery(this).find("td");
        jQuery(this).find("input:text").blur(function() {
            var tmp = jQuery.trim(jQuery(this).val());
            if (tmp == "")
            { jQuery($td[resultCol]).text(""); return; }
            if (isNaN(tmp)) {
                //非数字类型时，自动还原前次内容
                jQuery(this).val(jQuery(this).data("old_value"));
                return;
            }
            else {
                jQuery(this).val(getNum(jQuery(this).val()));
                jQuery(this).data("old_value", jQuery(this).val());
            }
            var sum = 0;
            var num1, num2;
            num1 = getNum(jQuery.trim(jQuery($td[col1]).text())) + getNum(jQuery.trim(jQuery($td[col1]).find("input:text:not(.none)").val()));
            num2 = getNum(jQuery.trim(jQuery($td[col2]).text())) + getNum(jQuery.trim(jQuery($td[col2]).find("input:text:not(.none)").val()));
            switch (operation) {
                case "min":
                    sum = num1 - num2; break;
                case "mul":
                    sum = num1 * num2; break;
                case "div":
                    sum = num1 / num2; break;
                default:
                    sum = num1 + num2;
            }
            sum = sum.toFixed(decCount);
            //判断结果列是否是文本框
            if (jQuery($td[resultCol]).find("input:text").size())
            { jQuery($td[resultCol]).find("input:text").val(sum); }
            else
            { jQuery($td[resultCol]).text(sum); }
        }
);
    }
);
}



var lstid;
var divMesssageId = "divFloatMessage";
function showMessage(s) {
    window.clearTimeout(lstid);
    var doc = window.top.document;
    var divmsg = doc.getElementById(divMesssageId);
    if (divmsg == null) {
        var newEle = doc.createElement("div");
        newEle.id = divMesssageId;
        doc.body.appendChild(newEle);
        divmsg = doc.getElementById(divMesssageId);
    }
    divmsg.innerHTML = s;
    divmsg.style.top = doc.documentElement.scrollTop + "px";
    divmsg.style.display = "block";

    lstid = window.setTimeout("hideMessage();", 5000);

}

function hideMessage() {
    var doc = window.top.document;
    var divmsg = doc.getElementById(divMesssageId);
    divmsg.style.display = "none";
}

function loadjs(url) {
    var id = url.replace(/[\W]/g, "_");
    var script = document.getElementById(id);
    if (script == null) {
        script = document.createElement("script");
        script.type = "text/javascript";
        script.src = rooturl + url;
        script.id = id;
        document.write(script.outerHTML);
    }
}

$(function() {
    $(".openlink a").each(function() {
        var url = $(this).attr("href");
        $(this).attr("href", "javascript:;");
        $(this).attr("href2", url);
    }).click(function() {
        var url = $(this).attr("href2");
        showModalPage2(url, 830, 600);
    });
});